package com.then.libary.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.then.libary.activity.HOTArticleActivity;
import com.then.libary.activity.R;
import com.then.libary.bean.Article;
import com.then.libary.bean.Column;
import com.then.libary.parse.ParseColumnJson;
import com.then.libary.service.HotArticleActivityService;
import com.then.libary.service.HotArticleService;
import com.then.libary.thread.MyGetMessageThread;
import com.then.libary.thread.ThreadExecutorPool;
import com.then.libary.util.AllConfig;

public class HotArticleActivityServiceImpl implements HotArticleActivityService {
	private ImageView head_left,head_right;
	private HOTArticleActivity hotArticleActivity;
	private LinearLayout layout;
	private ViewPager viewPager;
	private List<View> contentViews;
	private int pages = 10;
	private List<Article> articles;
	private List<HotArticleService> hoList;
	private boolean flag = false;
	private HotArticleAdapter hotArticleAdapter;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				parseHotArticle(msg.getData().getString("message"));
				break;

			default:
				break;
			}
		};
	};

	public HotArticleActivityServiceImpl(HOTArticleActivity hotArticleActivity) {
		// TODO Auto-generated constructor stub
		this.hotArticleActivity = hotArticleActivity;
		initUi();
		initData();
	}

	private List<Column> cList;
	private void parseHotArticle(String string) {
		// TODO Auto-generated method stub
		ParseColumnJson parseColumnJson = new ParseColumnJson();
		try {
			 cList = parseColumnJson.parseJsonColumnArticle(string);
			int size = cList.size();
			Log.e("size", "" + size);
			if (size == pages) {

			} else {

				if (pages != size) {
					
					pages = size;
					List<View> tmp = new ArrayList<View>(11);
					for (View view:contentViews) {
						tmp.add(view);
					}
					contentViews.clear();
					for (int i = 0; i < size; i++) {
						contentViews.add(tmp.get(i));
					}
				
				}

			}
			hotArticleAdapter = new HotArticleAdapter();
			viewPager.setAdapter(hotArticleAdapter);
			viewPager.setOnPageChangeListener(new HotAticleOnPageChangeListener());
			hotArticleAdapter.notifyDataSetChanged();
			viewPager.setVisibility(View.VISIBLE);
			layout.setVisibility(View.GONE);
			
			viewPager.setCurrentItem(0);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private View findViewById(int id) {
		return hotArticleActivity.findViewById(id);
	}

	private void initData() {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();
		map.put("search_EQ_JinfoAttrs.Jattribute.id", 7 + "");
		map.put("html_flag", "true");
		map.put("url", AllConfig.DOMAINNAME + "column_article.jspx");
		MyGetMessageThread myGetMessageThread = new MyGetMessageThread(handler,
				"get", map, 1);
		ThreadExecutorPool.executeThread(myGetMessageThread);

	}

	private void initUi() {
		// TODO Auto-generated method stub
		head_left = (ImageView) findViewById(R.id.all_head_bg_img);
		head_left.setBackgroundResource(R.drawable.hot_head_logo);
		head_right = (ImageView) findViewById(R.id.head_menu_btn);
		head_right.setBackgroundResource(R.drawable.head_back);
		head_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				hotArticleActivity.finish();
			}
		});
		contentViews = new ArrayList<View>();
		hoList = new ArrayList<HotArticleService>();
		layout = (LinearLayout) findViewById(R.id.hot_article_container_linearLayout);
		viewPager = (ViewPager) findViewById(R.id.hot_article_pager);
		for (int i = 0; i < pages; i++) {
			View view = hotArticleActivity.getLayoutInflater().inflate(
					R.layout.hot_article, null);
			contentViews.add(view);
			hoList.add(new HotArticleServiceImpl(view, hotArticleActivity));
		}
		
	}

	private class HotArticleAdapter extends PagerAdapter {
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub

			container.removeView(contentViews.get(position));
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return contentViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			container.addView(contentViews.get(position), 0);
			HotArticleService hotArticleService = hoList.get(position);
			hotArticleService.init(cList.get(position));
			return contentViews.get(position);
		}
	}

	private class HotAticleOnPageChangeListener implements OnPageChangeListener
	{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int location) {
			// TODO Auto-generated method stub
			
		}
		
	}
	/*
	 * private class HotAticleOnPageChangeListener implements
	 * OnPageChangeListener {
	 * 
	 * @Override public void onPageScrollStateChanged(int arg0) { // TODO
	 * Auto-generated method stub
	 * 
	 * }
	 * 
	 * @Override public void onPageScrolled(int arg0, float arg1, int arg2) { //
	 * TODO Auto-generated method stub
	 * 
	 * }
	 * 
	 * @Override public void onPageSelected(int location) { // TODO
	 * Auto-generated method stub View parentView = contentViews.get(location);
	 * HotArticleService hotArticleService = hoList.get(location); if
	 * (hotArticleService == null) { hotArticleService = new
	 * HotArticleServiceImpl(parentView, hotArticleActivity); }
	 * 
	 * }
	 * 
	 * }
	 */

}
